package org.opens.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能:
 *      全局异常处理器;
 * 说明:
 *      1. @ExceptionHandler注解用来说明发生那个异常时会走这个处理器.
 */
@ControllerAdvice //@ControllerAdvice 该注解定义全局异常处理类
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class) //@ExceptionHandler 该注解声明异常处理方法
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        //异常内容(页面展示)
        mav.addObject("exception", e);
        //请求的url地址(页面展示)
        mav.addObject("url", req.getRequestURL());
        //设置视图名称
        mav.setViewName("error/errorPage");
        return mav;
    }

}
